<?php
/**
 * Created by PhpStorm.
 * User: madong
 * Date: 2018/9/16
 * Time: 下午6:21
 */

namespace app\api\controller\jiaoyi;

use app\api\controller\Common;
use app\api\model\Adeth;
use think\Config;
use think\Controller;
use think\Db;
use think\Exception;
use think\Request;
use think\Session;
use app\api\model\User;
use app\api\model\Jiaoyi;
use app\api\model\JiaoyiUinfo;

class Changwai extends Controller
{
    private $coin_host = '47.92.195.55';//钱包服务器IP

    private $gas = 0.0005;//手续费

    private $xiao = 50;//卖出最小限制


    //根据账号查询当前账户的实际余额(接口查询)

    protected function Get_Balance($uname = '', $coin_type = ''){

        $coin_user_obj = new Adeth();

        $user_address = $coin_user_obj->where('uname',$uname)->value('address');

        if ($coin_type == 'eth'){

            $balance = json_decode(file_get_contents('http://'.$this->coin_host.':8883/?type=4&account='.$user_address))->Balance;

        }else{

            $balance = json_decode(file_get_contents('http://'.$this->coin_host.':8882/?type=token_balance&con_addr='.Config::get('con_'.$coin_type).'&account='.$user_address))->balance;

        }

        return $balance;

    }

    //获取订单
    public function Get_order(){

        //echo json_encode(['zt'=>0,'msg'=>'当前没有新订单可查询']);die();

        $Jiaoyi_obj = new Jiaoyi();

        $res = Request::instance();

        $order_buy_info = $Jiaoyi_obj->where('type',$res->param('type'))->where('bizhong',$res->param('bizong'))->where('zt',1)->select();

        static $data_arr = array();

        foreach ($order_buy_info as $value) {

            $yuer = Db::name('jiaoyiUinfo')->where('uname',$value['uname'])->value($value['bizhong']);

            if ($value['type'] == 0){

                $arr = array();
                $arr['id'] = $value['id'];
                $arr['uname'] = $value['uname'];
                $arr['uid'] = $value['uid'];
                $arr['value'] = $value['value'];
                $arr['sysl'] = $value['sysl'];
                $arr['jiage'] = $value['jiage'];
                $arr['xiao'] = $value['xiao'];
                $arr['da'] = $value['da'];
                $arr['truename'] = $value['truename'];
                $arr['yinlian'] = $value['yinlian'];
                $arr['khh'] = $value['khh'];
                $arr['zfb'] = $value['zfb'];
                $arr['weixin'] = $value['weixin'];
                $arr['bizhong'] = $value['bizhong'];
                $arr['type'] = $value['type'];
                $arr['typedescribe'] = $value['typedescribe'];
                $arr['ordernumber'] = $value['ordernumber'];
                $arr['note'] = $value['note'];
                $arr['weixinpic'] = $value['weixinpic'];
                $arr['zfbpic'] = $value['zfbpic'];
                $arr['touxiang'] = $value->UserInfo->headimg;
                $arr['jiaoyics'] = $value['jiaoyics'];
                $arr['xinren'] = $value['xinren'];
                $arr['haoping'] = $value['haoping'];
                $arr['time'] = $value['time'];
                $arr['wfkdd'] = $value['wfkdd'];
                $arr['yfkdd'] = $value['yfkdd'];
                $arr['jiaoyiok'] = $value['jiaoyiok'];
                $arr['zt'] = $value['zt'];
                $arr['imgurl'] = "http://".$_SERVER['HTTP_HOST'];
                $data_arr[]=$arr;

            }else{

                if ($yuer >=  $value['xiao']){

                    $arr = array();
                    $arr['id'] = $value['id'];
                    $arr['uname'] = $value['uname'];
                    $arr['uid'] = $value['uid'];
                    $arr['value'] = $value['value'];
                    $arr['sysl'] = $value['sysl'];
                    $arr['jiage'] = $value['jiage'];
                    $arr['xiao'] = $value['xiao'];
                    $arr['da'] = $value['da'];
                    $arr['truename'] = $value['truename'];
                    $arr['yinlian'] = $value['yinlian'];
                    $arr['khh'] = $value['khh'];
                    $arr['zfb'] = $value['zfb'];
                    $arr['weixin'] = $value['weixin'];
                    $arr['bizhong'] = $value['bizhong'];
                    $arr['type'] = $value['type'];
                    $arr['typedescribe'] = $value['typedescribe'];
                    $arr['ordernumber'] = $value['ordernumber'];
                    $arr['note'] = $value['note'];
                    $arr['weixinpic'] = $value['weixinpic'];
                    $arr['zfbpic'] = $value['zfbpic'];
                    $arr['touxiang'] = $value->UserInfo->headimg;
                    $arr['jiaoyics'] = $value['jiaoyics'];
                    $arr['xinren'] = $value['xinren'];
                    $arr['haoping'] = $value['haoping'];
                    $arr['time'] = $value['time'];
                    $arr['wfkdd'] = $value['wfkdd'];
                    $arr['yfkdd'] = $value['yfkdd'];
                    $arr['jiaoyiok'] = $value['jiaoyiok'];
                    $arr['zt'] = $value['zt'];
                    $arr['imgurl'] = "http://".$_SERVER['HTTP_HOST'];
                    $data_arr[]=$arr;

                }

            }


        }

        if ($data_arr){

            echo json_encode(['zt'=>1,'msg'=>$this->Jiami(json_encode($data_arr))]);

        }else{

            echo json_encode(['zt'=>0,'msg'=>'当前没有新订单可查询']);

        }

    }
    /*
     * 根据订单号查询
     *
     * @$ordernum  传入订单号
     *
     */
    public function Get_order_info($ordernum){

        $order_obj = new Jiaoyi();

        $order_buy_info = $order_obj->where('ordernumber',$ordernum)->find();

        echo json_encode($order_buy_info);

    }

    /*
     * 获取行情价格（美元）
     *
     */

    public function Get_price(){

        $data = [

            'eth' => json_decode(file_get_contents('https://api.coinmarketcap.com/v2/ticker/1027/'),true)['data']['quotes']['USD']['price'],

            'ae' => json_decode(file_get_contents('https://api.coinmarketcap.com/v2/ticker/1700/'),true)['data']['quotes']['USD']['price'],

            'btm' => json_decode(file_get_contents('https://api.coinmarketcap.com/v2/ticker/1866/'),true)['data']['quotes']['USD']['price'],

            'snt' => json_decode(file_get_contents('https://api.coinmarketcap.com/v2/ticker/1759/'),true)['data']['quotes']['USD']['price'],

            'iost' => json_decode(file_get_contents('https://api.coinmarketcap.com/v2/ticker/2405/'),true)['data']['quotes']['USD']['price'],

            'zrx' => json_decode(file_get_contents('https://api.coinmarketcap.com/v2/ticker/1896/'),true)['data']['quotes']['USD']['price'],

            'zil' => json_decode(file_get_contents('https://api.coinmarketcap.com/v2/ticker/2469/'),true)['data']['quotes']['USD']['price']

        ];

        echo json_encode($data);

    }
    //更新行情数据库行情数据
    public function update_hangqing(){

        //$huilv = Db::name('hangqing')->where('id',1)->value('huilv');

        $data = [

            'eth' => json_decode(file_get_contents('https://api.coinmarketcap.com/v2/ticker/1027/'),true)['data']['quotes']['USD']['price'],

            'ae' => json_decode(file_get_contents('https://api.coinmarketcap.com/v2/ticker/1700/'),true)['data']['quotes']['USD']['price'],

            'btm' => json_decode(file_get_contents('https://api.coinmarketcap.com/v2/ticker/1866/'),true)['data']['quotes']['USD']['price'],

            'snt' => json_decode(file_get_contents('https://api.coinmarketcap.com/v2/ticker/1759/'),true)['data']['quotes']['USD']['price'],

            'iost' => json_decode(file_get_contents('https://api.coinmarketcap.com/v2/ticker/2405/'),true)['data']['quotes']['USD']['price'],

            'zrx' => json_decode(file_get_contents('https://api.coinmarketcap.com/v2/ticker/1896/'),true)['data']['quotes']['USD']['price'],

            'zil' => json_decode(file_get_contents('https://api.coinmarketcap.com/v2/ticker/2469/'),true)['data']['quotes']['USD']['price']

        ];

        if (Db::name('hangqing')->where('id',1)->update($data)){

            echo json_encode(['zt'=>1,'msg'=>'行情更新成功']);

        }else{

            echo json_encode(['zt'=>0,'msg'=>'行情更新失败']);

        };

    }
    //货币行情读取
    public function Get_hangqing(){

        $data = Db::name('hangqing')->where('id',1)->find();

        if($data){

            echo json_encode(['zt'=>1,'msg'=>'行情获取成功','data'=>$data]);

        }else{

            echo json_encode(['zt'=>0,'msg'=>'行情获取','data'=>$data]);

        }

    }
    // //获取当前美元汇率放到数据库（更新操作）服务器已定时执行
    // public function Get_huilv(){

    //     $data = json_decode(file_get_contents('http://web.juhe.cn:8080/finance/exchange/rmbquot?key=4b2ffaf0eb01eedec87f4033d6c53404'))->result[0]->data1->bankConversionPri/100;

    //     $hlerr = json_decode(file_get_contents('http://web.juhe.cn:8080/finance/exchange/rmbquot?key=4b2ffaf0eb01eedec87f4033d6c53404'))->error_code;

    //     if ($hlerr){

    //         echo '接口错误，错误代码 '.$hlerr;

    //         die();

    //     }

    //     if (Db::name('hangqing')->where('id',1)->update(['huilv'=>$data])){

    //         echo '汇率更新成功'.$data.'更新时间'.date('Y-m-d H:i:s',time());

    //     }else{

    //         echo '数据没有变化,当前获取的最新价格'.$data;

    //     }

    // }
    //获取指定币种的行情（不带汇率的）
    public function Get_coin_type($type='eth'){

        $huilv = Db::name('hangqing')->where('id',1)->value('huilv');

        $data = Db::name('hangqing')->where('id',1)->value($type);

        if ($data){

            echo json_encode(['zt'=>1,'msg'=>$data*$huilv]);

        }else{

            echo json_encode(['zt' =>0,'msg'=>'行情获取失败']);

        }

    }
    //获取指定币种的行情(带汇率的)
    public function Get_coin_type_huilv(){

        $res = Request::instance();

        $huilv = Db::name('hangqing')->where('id',1)->value('huilv');

        $data = Db::name('hangqing')->where('id',1)->value($res->param('type'));

        if ($data){

            echo json_encode(['zt'=>1,'msg'=>$data*$huilv]);

        }else{

            echo json_encode(['zt' =>0,'msg'=>'行情获取失败']);

        }

    }
    //获取交易所账户余额
    public function Get_jiaoyi_yuer(){

        $res = Request::instance();

        $data = Db::name('jiaoyi_uinfo')->where('uname',$res->param('user'))->value($res->param('coin_type'));

        if ($data){

            echo json_encode(['zt'=>1,'msg'=>$data]);

        }else{

            echo json_encode(['zt'=>0,'msg'=>'获取余额失败']);

        }

    }

    //数据解密
    private function Jiemi($data){

        $jiami_data = openssl_decrypt(base64_decode($data,false),"AES-128-CBC",Config::get('Key'),OPENSSL_RAW_DATA,Config::get('IV'));

        return $jiami_data;

    }
    //数据加密
    private function Jiami($data){

        $jiemi_data =  base64_encode(openssl_encrypt($data,"AES-128-CBC",Config::get('Key'),OPENSSL_RAW_DATA,Config::get('IV')));

        return $jiemi_data;

    }

}